<template>
	<div class="system-operlog-container">
		<el-card shadow="hover" class="mb10" v-auth="'monitor:operlog:list'">
			<el-input size="small" v-model="search.title" placeholder="请输入操作模块" prefix-icon="el-icon-search" style="max-width: 180px;" class="ml10"></el-input>
			<el-input size="small" v-model="search.operName" placeholder="请输入操作人员" prefix-icon="el-icon-search" style="max-width: 180px;" class="ml10"></el-input>
			<el-select size="small" v-model="search.businessType" placeholder="请选择操作类型" clearable style="max-width: 180px;" class="ml10">
				<el-option value="1" label="新增"></el-option>
				<el-option value="2" label="修改"></el-option>
				<el-option value="3" label="删除"></el-option>
				<el-option value="4" label="授权"></el-option>
				<el-option value="5" label="导出"></el-option>
				<el-option value="6" label="导入"></el-option>
				<el-option value="7" label="强退"></el-option>
				<el-option value="8" label="生成"></el-option>
				<el-option value="9" label="清空"></el-option>
				<el-option value="0" label="其他"></el-option>
			</el-select>
			<el-select size="small" v-model="search.status" placeholder="请选择操作状态" clearable style="max-width: 180px;" class="ml10">
				<el-option value="0" label="成功"></el-option>
				<el-option value="1" label="失败"></el-option>
			</el-select>
			<el-date-picker size="small" type="daterange" align="right" v-model="dateData.value" :shortcuts="dateData.shortcuts" 
				unlink-panels range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" class="ml10"></el-date-picker>
			<el-button size="small" icon="el-icon-search" type="primary" class="ml10" @click="onTableSearch()">查询</el-button>
			<el-button size="small" icon="el-icon-refresh" @click="onResetSearch()">重置</el-button>
		</el-card>
		<el-card shadow="hover">
			<el-button v-auth="'monitor:operlog:remove'" size="small" type="primary" class="ml10" icon="el-icon-delete" @click="onTabelRowsDel()" :disabled="tableData.selections.length <= 0">删除</el-button>
			<el-button v-auth="'monitor:operlog:clean'" size="small" type="danger" class="ml10" icon="el-icon-delete" @click="onTabelRowsClean()">清空</el-button>
			<el-table ref="tableOperlogRef" :data="tableData.data" @selection-change="onTableSelectionChange" style="width: 100%" row-key="id">
				<el-table-column type="selection" align="cneter" width="50" show-overflow-tooltip></el-table-column>
				<el-table-column type="index" align="cneter" width="50" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作模块" prop="title" show-overflow-tooltip></el-table-column>
				<el-table-column label="请求地址" prop="operUrl" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作人员" prop="operName" width="150" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作类型" prop="businessType" width="100" show-overflow-tooltip>
					<template #default="scope">
						<span v-if="scope.row.businessType == '1'" class="color-primary">新增</span>
						<span v-else-if="scope.row.businessType == '2'" class="color-success">修改</span>
						<span v-else-if="scope.row.businessType == '3'" class="color-danger">删除</span>
						<span v-else-if="scope.row.businessType == '4'" class="color-warning">授权</span>
						<span v-else-if="scope.row.businessType == '5'" class="color-info">导出</span>
						<span v-else-if="scope.row.businessType == '6'" class="color-info">导入</span>
						<span v-else-if="scope.row.businessType == '7'" class="color-warning">强退</span>
						<span v-else-if="scope.row.businessType == '8'" class="color-warning">生成</span>
						<span v-else-if="scope.row.businessType == '9'" class="color-danger">清空</span>
						<span v-else class="color-info">其他</span>
					</template>
				</el-table-column>
				<el-table-column label="操作状态" prop="status" width="100" show-overflow-tooltip>
					<template #default="scope">
						<span v-if="scope.row.status == '0'" class="color-success">成功</span>
						<span v-else class="color-danger">失败</span>
					</template>
				</el-table-column>
				<el-table-column label="操作地址" prop="operIp" width="180" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作地点" prop="operLocation" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作时间" prop="createTime" width="180" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" show-overflow-tooltip width="125"
					v-auths="['monitor:operlog:detail', 'monitor:operlog:remove']">
					<template #default="scope">
						<el-button v-auth="'monitor:operlog:detail'" size="mini" type="text" @click="onOpenDetailOperlog(scope.row)">详情</el-button>
						<el-button v-auth="'monitor:operlog:remove'" size="mini" type="text" @click="onTabelRowDel(scope.row)" >删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onPageSizeChange"
				@current-change="onPageCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30, 40, 50, 60]"
				v-model:current-page="search.pageNum"
				background
				v-model:page-size="search.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="tableData.total"
			>
			</el-pagination>
		</el-card>
		<DetailOperlog ref="detailOperlogRef"/>
	</div>
</template>

<script lang="ts">
import { ref, toRefs, reactive, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import DetailOperlog from '/@/views/monitor/operlog/detailOperlog.vue';
import { getOperlogList, delOperlog, cleanOperlog } from '/@/api/monitor/operlog/index';
import { formatDate, getDateRangeShortcuts } from '/@/utils/formatTime';
export default {
	name: 'operlog',
	components: { DetailOperlog },
	setup() {
		const tableOperlogRef = ref();
		const detailOperlogRef = ref();
		const state = reactive({
			search:{
				title: '',
				operName: '',
				businessType: '',
				status: '',
				startTime: '',
				endTime: '',
				orderBy: 'createTime',
				isAsc: 'desc',
				pageNum: 1,
				pageSize: 10
			},
			dateData: {
				value: [],
				shortcuts: getDateRangeShortcuts()
			},
			tableData: {
				selections: [],//选中的行数据
				data: [],
				total: 0,
				loading: false
			}
		});

		const loadTableData = (params) => {
			getOperlogList(Object.assign(state.search, params)).then((res) => {
				if(res.code == 0){
					state.tableData.data = res.data;
					state.tableData.total = res.total;
					tableOperlogRef.value.clearSelection();
				}
			});
		}

		//查询
		const onTableSearch = () => {
			if(state.dateData.value.length == 2){
				state.search.startTime = formatDate(state.dateData.value[0], 'yyyy-MM-dd') + ' 00:00:00';
				state.search.endTime = formatDate(state.dateData.value[1], 'yyyy-MM-dd') + ' 23:59:59';
			}
			loadTableData(state.search);
		}
		//重置
		const onResetSearch = () => {
			state.dateData.value = [];
			state.search.title = undefined;
			state.search.operName = undefined;
			state.search.businessType = undefined;
			state.search.status = undefined;
			state.search.startTime = undefined;
			state.search.endTime = undefined;
			state.search.pageNum = 1;
			state.search.pageSize = 10;
			loadTableData(state.search);
		}
		// 监听表格选中行
		const onTableSelectionChange = (selections) =>{
			state.tableData.selections = selections;
		};
		// 每页显示数据大小改变
		const onPageSizeChange = (val: number) => {
			state.search.pageSize = val;
			loadTableData(state.search);
		};
		// 页码改变
		const onPageCurrentChange = (val: number) => {
			state.search.pageNum = val;
			loadTableData(state.search);
		};
		// 打开操作日志详情弹窗
		const onOpenDetailOperlog = (row: object) => {
			detailOperlogRef.value.openDialog(row);
		};
	
		// 删除选中行
		const onTabelRowsDel = () => {
			const ids = state.tableData.selections.map(item => item.id);
			if(ids && ids.length > 0){
				ElMessageBox.confirm('确定删除选中的操作日志信息吗？', '提示', {
					confirmButtonText: '删除',
					cancelButtonText: '取消',
					type: 'warning',
				}).then(() => {
					delOperlog(ids).then((res) => {
						if(res.code === 0){
							ElMessage.success('删除成功');
							loadTableData();
						}
					});
				}).catch(() => {});
			}
		};
		// 删除当前行
		const onTabelRowDel = (row: object) => {
			ElMessageBox.confirm('确定删除该条操作日志信息吗？', '提示', {
				confirmButtonText: '删除',
				cancelButtonText: '取消',
				type: 'warning',
			}).then(() => {
				delOperlog([row.id]).then((res) => {
					if(res.code === 0){
						ElMessage.success('删除成功');
						loadTableData();
					}
				});
			}).catch(() => {});
		};
		// 清空
		const onTabelRowsClean = () =>{
			ElMessageBox.confirm('确定清空操作日志信息吗？', '提示', {
				confirmButtonText: '清空',
				cancelButtonText: '取消',
				type: 'warning',
			}).then(() => {
				cleanOperlog().then((res) => {
					if(res.code === 0){
						ElMessage.success('清空成功');
						loadTableData();
					}
				})
			});
		}
		// 页面加载时
		onMounted(() => {
			loadTableData();
		});
		return {
			tableOperlogRef,
			detailOperlogRef,
			onTableSearch,
			onTableSelectionChange,
			onResetSearch,
			onPageSizeChange,
			onPageCurrentChange,
			onOpenDetailOperlog,
			onTabelRowsDel,
			onTabelRowDel,
			onTabelRowsClean,
			...toRefs(state),
		};
	},
};
</script>